﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using Transport.Areas.Administrator.Models;
using Transport.Core.Models.Settings;
using Transport.Infrastructure.Mvc;
using Transport.Infrastructure.Security;
using Transport.Services;
using Transport.Services.Infrastructure;
using Transport.Services.Settings;
using Transport.Services.Users;

namespace Transport.Areas.Administrator.Controllers
{
    [SuperAdminAuthorize]
    public class MyOfficeController : ControllerBase<IMyOfficeRepository, MyOffice>
    {
        private readonly IUserRepository _userRepository;
        private readonly IRatesTableRepository _ratesTableRepository;

        public MyOfficeController(IUnitOfWork unitOfWork, IMyOfficeRepository repository, IUserRepository userRepository,
                                  IRatesTableRepository ratesTableRepository)
            : base(repository, unitOfWork)
        {
            _userRepository = userRepository;
            _ratesTableRepository = ratesTableRepository;
        }

        //
        // GET: /Administrator/MyOffice/

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GridModel(string search)
        {
            var model = Repository.Search(search);

            var gridModel = new GridModel<MyOfficeModel>
            {
                Data = model.Select(x => new MyOfficeModel
                                             {
                                                 Address = x.Address,
                                                 MyOfficeId = x.MyOfficeId,
                                                 OfficeName = x.OfficeName,
                                                 PhoneNumber = x.PhoneNumber,
                                                 Fax = x.Fax
                                             })
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        public ActionResult Create()
        {
            var model = new MyOfficeModel();
            return View(model);
        }

        public virtual ActionResult Edit(int id)
        {
            var entity = Repository.GetById(id);
            var model = new MyOfficeModel()
            {
                Address = entity.Address,
                MyOfficeId = entity.MyOfficeId,
                OfficeName = entity.OfficeName,
                PhoneNumber = entity.PhoneNumber,
                Fax = entity.Fax
            };
            return View("Edit", model);
        }

        [HttpPost, ValidateInput(false)]
        public virtual ActionResult Save(MyOfficeModel myOfficeModel)
        {
            if (myOfficeModel.MyOfficeId <= 0) //Create News
            {
                if (!ModelState.IsValid)
                {
                    return View("Create", myOfficeModel);
                }
                var myOffice = new MyOffice()
                {
                    IsDeleted =false,
                    Address = myOfficeModel.Address,
                    OfficeName = myOfficeModel.OfficeName,
                    PhoneNumber = myOfficeModel.PhoneNumber,
                    Fax = myOfficeModel.Fax
                };
                using (UnitOfWork)
                {
                    Repository.Insert(myOffice);
                }
                //Create default rate table
                using (UnitOfWork)
                {
                    CreateRateTables(myOffice);
                }
            }
            else //Edit user
            {
                if (!ModelState.IsValid)
                {
                    return View("Edit", myOfficeModel);
                }

                var myOffice = Repository.GetById(myOfficeModel.MyOfficeId);
                myOffice.Address = myOfficeModel.Address;
                myOffice.OfficeName = myOfficeModel.OfficeName;
                myOffice.PhoneNumber = myOfficeModel.PhoneNumber;
                myOffice.Fax = myOfficeModel.Fax;
                using (UnitOfWork)
                {
                    Repository.Update(myOffice);
                }
            }

            //Save success
            this.SetSuccessNotification(string.Format("{0} đã được lưu thành công.", "Trung tâm"));
            return RedirectToAction("Index", new { area = "Administrator" });
        }

        public ActionResult Delete(int id)
        {
            try
            {
                using (UnitOfWork)
                {
                    var entity = Repository.GetById(id);
                    entity.IsDeleted = true;
                    var users = _userRepository.GetAll().Where(p => p.MyOfficeId == id);
                    foreach (var user in users)
                    {
                        user.IsDeleted = true;
                    }
                    
                }
                this.SetSuccessNotification("Trung tâm đã được xóa thành công.");
            }
            catch
            {
                this.SetErrorNotification("Trung tâm này không thể xóa, vì đã được sử dụng!");
            }
            return RedirectToAction("index", new { area = "Administrator" });
        }

        private void CreateRateTables(MyOffice myOffice)
        {
            var pages = new List<RatesTable>
                            {
                                new RatesTable()
                                {
                                    Description = "Đến 50",
                                    IsExtraColumn = false,
                                    MaxWeight = 50,
                                    MinWeight = 0,
                                    PriceColumnA = 8000,
                                    PriceColumnB = 12500,
                                    PriceColumnC = 12000,
                                    PriceColumnD = 12500,
                                    PriceColumnE = 10500,
                                    WeightOver = 0,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },
                                new RatesTable()
                                {
                                    Description = "Trên 50 => 100",
                                    IsExtraColumn = false,
                                    MaxWeight = 100,
                                    MinWeight = 50,
                                    PriceColumnA = 10200,
                                    PriceColumnB = 15000,
                                    PriceColumnC = 15500,
                                    PriceColumnD = 17000,
                                    PriceColumnE = 12500,
                                    WeightOver = 0,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },
                                new RatesTable()
                                {
                                    Description = "Trên 100 => 250",
                                    IsExtraColumn = false,
                                    MaxWeight = 250,
                                    MinWeight = 100,
                                    PriceColumnA = 12500,
                                    PriceColumnB = 17000,
                                    PriceColumnC = 20000,
                                    PriceColumnD = 24000,
                                    PriceColumnE = 16000,
                                    WeightOver = 0,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },
                                new RatesTable()
                                {
                                    Description = "Trên 250 => 500",
                                    IsExtraColumn = false,
                                    MaxWeight = 500,
                                    MinWeight = 250,
                                    PriceColumnA = 15000,
                                    PriceColumnB = 23000,
                                    PriceColumnC = 27500,
                                    PriceColumnD = 31500,
                                    PriceColumnE = 30000,
                                    WeightOver = 0,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },
                                new RatesTable()
                                {
                                    Description = "Trên 500 => 1000",
                                    IsExtraColumn = false,
                                    MaxWeight = 50,
                                    MinWeight = 0,
                                    PriceColumnA = 17000,
                                    PriceColumnB = 32000,
                                    PriceColumnC = 38500,
                                    PriceColumnD = 45500,
                                    PriceColumnE = 41500,
                                    WeightOver = 0,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },
                                new RatesTable()
                                {
                                    Description = "Trên 1000 => 1500",
                                    IsExtraColumn = false,
                                    MaxWeight = 1500,
                                    MinWeight = 1000,
                                    PriceColumnA = 22000,
                                    PriceColumnB = 41500,
                                    PriceColumnC = 47000,
                                    PriceColumnD = 58000,
                                    PriceColumnE = 53000,
                                    WeightOver = 0,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },
                                new RatesTable()
                                {
                                    Description = "Trên 1500 => 2000",
                                    IsExtraColumn = false,
                                    MaxWeight = 2000,
                                    MinWeight = 1500,
                                    PriceColumnA = 26500,
                                    PriceColumnB = 49500,
                                    PriceColumnC = 56000,
                                    PriceColumnD = 69500,
                                    PriceColumnE = 63000,
                                    WeightOver = 0,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },
                                new RatesTable()
                                {
                                    Description = "500 gr tiếp theo",
                                    IsExtraColumn = true,
                                    MaxWeight = 0,
                                    MinWeight = 0,
                                    PriceColumnA = 2800,
                                    PriceColumnB = 5000,
                                    PriceColumnC = 6100,
                                    PriceColumnD = 9000,
                                    PriceColumnE = 8000,
                                    WeightOver = 500,
                                    IsDeleted =false,
                                    ModifiedByUserId = 1,
                                    ModifiedDate = DateTime.Now,
                                    MyOfficeId = myOffice.MyOfficeId
                                },

                            };

            pages.ForEach(c => _ratesTableRepository.Insert(c));
        }
    }
}
